<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册 - 在线影院订票系统</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 2rem 0;
        }
        .register-container {
            background: white;
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            overflow: hidden;
        }
        .register-header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 2rem;
            text-align: center;
        }
        .register-form {
            padding: 2rem;
        }
        .form-control {
            border-radius: 10px;
            border: 2px solid #e9ecef;
            padding: 12px 15px;
        }
        .form-control:focus {
            border-color: #667eea;
            box-shadow: 0 0 0 0.2rem rgba(102, 126, 234, 0.25);
        }
        .btn-register {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border: none;
            border-radius: 10px;
            padding: 12px;
            font-weight: 600;
            text-transform: uppercase;
            letter-spacing: 1px;
        }
        .btn-register:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }
        .alert {
            border-radius: 10px;
        }
        .required {
            color: #dc3545;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8 col-lg-6">
                <div class="register-container">
                    <div class="register-header">
                        <h2><i class="fas fa-user-plus me-2"></i>用户注册</h2>
                        <p class="mb-0">加入我们，享受精彩电影</p>
                    </div>
                    
                    <div class="register-form">
                        <!-- 错误信息 -->
                        <c:if test="${not empty error}">
                            <div class="alert alert-danger" role="alert">
                                <i class="fas fa-exclamation-triangle me-2"></i>${error}
                            </div>
                        </c:if>
                        
                        <!-- 提示信息 -->
                        <c:if test="${not empty message}">
                            <div class="alert alert-info" role="alert">
                                <i class="fas fa-info-circle me-2"></i>${message}
                            </div>
                        </c:if>
                        
                        <form action="${pageContext.request.contextPath}/register" method="post" id="registerForm">
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="username" class="form-label">
                                        <i class="fas fa-user me-1"></i>用户名 <span class="required">*</span>
                                    </label>
                                    <input type="text" class="form-control" id="username" name="username" 
                                           value="${username}" required placeholder="3-20个字符">
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="phone" class="form-label">
                                        <i class="fas fa-phone me-1"></i>手机号 <span class="required">*</span>
                                    </label>
                                    <input type="tel" class="form-control" id="phone" name="phone" 
                                           value="${phone}" required placeholder="用于生成订单号">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="password" class="form-label">
                                        <i class="fas fa-lock me-1"></i>密码 <span class="required">*</span>
                                    </label>
                                    <input type="password" class="form-control" id="password" name="password" 
                                           required placeholder="6-20个字符">
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="confirmPassword" class="form-label">
                                        <i class="fas fa-lock me-1"></i>确认密码 <span class="required">*</span>
                                    </label>
                                    <input type="password" class="form-control" id="confirmPassword" name="confirmPassword" 
                                           required placeholder="再次输入密码">
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="email" class="form-label">
                                        <i class="fas fa-envelope me-1"></i>邮箱
                                    </label>
                                    <input type="email" class="form-control" id="email" name="email" 
                                           value="${email}" placeholder="可选">
                                </div>
                                
                                <div class="col-md-6 mb-3">
                                    <label for="realName" class="form-label">
                                        <i class="fas fa-id-card me-1"></i>真实姓名
                                    </label>
                                    <input type="text" class="form-control" id="realName" name="realName" 
                                           value="${realName}" placeholder="可选">
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label class="form-label">
                                    <i class="fas fa-venus-mars me-1"></i>性别
                                </label>
                                <div class="d-flex gap-3">
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="gender" id="male" value="M" 
                                               ${gender == 'M' ? 'checked' : ''}>
                                        <label class="form-check-label" for="male">男</label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="gender" id="female" value="F" 
                                               ${gender == 'F' ? 'checked' : ''}>
                                        <label class="form-check-label" for="female">女</label>
                                    </div>
                                    <div class="form-check">
                                        <input class="form-check-input" type="radio" name="gender" id="other" value="OTHER" 
                                               ${gender == 'OTHER' || empty gender ? 'checked' : ''}>
                                        <label class="form-check-label" for="other">其他</label>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="d-grid mb-3">
                                <button type="submit" class="btn btn-primary btn-register">
                                    <i class="fas fa-user-plus me-2"></i>注册
                                </button>
                            </div>
                            
                            <div class="text-center">
                                <span class="text-muted">已有账号？</span>
                                <a href="${pageContext.request.contextPath}/login" class="text-decoration-none">
                                    立即登录
                                </a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            // 表单验证
            $('#registerForm').on('submit', function(e) {
                var username = $('#username').val().trim();
                var password = $('#password').val().trim();
                var confirmPassword = $('#confirmPassword').val().trim();
                var phone = $('#phone').val().trim();
                var email = $('#email').val().trim();
                
                // 必填项验证
                if (!username || !password || !phone) {
                    e.preventDefault();
                    alert('用户名、密码和手机号为必填项');
                    return false;
                }
                
                // 用户名长度验证
                if (username.length < 3 || username.length > 20) {
                    e.preventDefault();
                    alert('用户名长度必须在3-20个字符之间');
                    return false;
                }
                
                // 密码长度验证
                if (password.length < 6 || password.length > 20) {
                    e.preventDefault();
                    alert('密码长度必须在6-20个字符之间');
                    return false;
                }
                
                // 密码确认验证
                if (password !== confirmPassword) {
                    e.preventDefault();
                    alert('两次输入的密码不一致');
                    return false;
                }
                
                // 手机号格式验证
                var phoneRegex = /^1[3-9]\d{9}$/;
                if (!phoneRegex.test(phone)) {
                    e.preventDefault();
                    alert('请输入有效的手机号码');
                    return false;
                }
                
                // 邮箱格式验证（如果填写了）
                if (email && !/^[A-Za-z0-9+_.-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$/.test(email)) {
                    e.preventDefault();
                    alert('请输入有效的邮箱地址');
                    return false;
                }
            });
            
            // 密码确认实时验证
            $('#confirmPassword').on('blur', function() {
                var password = $('#password').val();
                var confirmPassword = $(this).val();
                
                if (password && confirmPassword && password !== confirmPassword) {
                    $(this).addClass('is-invalid');
                } else {
                    $(this).removeClass('is-invalid');
                }
            });
        });
    </script>
</body>
</html>
